/****************************************************************************
 * Copyright (C) 2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * SaveInfos.cpp
 *
 * for SaveGame Manager GX 2013
 ***************************************************************************/
#include "SaveInfos.hpp"
#include "../Themes/CTheme.h"
#include "../Menu/Explorer.h"
#include "../Saves/SaveTools.h"
#include "../FileOperations/Nand.hpp"
#include "../FileOperations/fileops.h"

SaveInfos::SaveInfos()
	: GuiFrame(0, 0)
{
	saveBanner = NULL;
	idBox = NULL;
	blockBox = NULL;
	databinInfos = NULL;
	m_tid = 0;
	m_id = "";
	
	nameTxt = new GuiText("", 35, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	nameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	nameTxt->SetPosition(30, 85);
	nameTxt->SetMaxWidth(500, SCROLL_HORIZONTAL);
	
	subnameTxt = new GuiText((char*) NULL, 22, thColor("r=255 g=255 b=255 a=255 - manage windows text color"));
	subnameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	subnameTxt->SetPosition(30, 123);
	subnameTxt->SetMaxWidth(500, SCROLL_HORIZONTAL);
	
	Append(nameTxt);
	Append(subnameTxt);
}

SaveInfos::~SaveInfos()
{
	RemoveAll();
	
	if(saveBanner)
		delete saveBanner;
	if(idBox)
		delete idBox;
	if(blockBox)
		delete blockBox;
	
	delete nameTxt;
	delete subnameTxt;
	
	if(databinInfos)
		delete databinInfos;
}

void SaveInfos::SetInfos(int index)
{
	saveSelected = index;
	
	if(Settings.LoadedBrowser != SDUSB_BROWSER)
	{
		nameTxt->SetText(CurrentBrowser()->GetSaveName(saveSelected));
		subnameTxt->SetText(CurrentBrowser()->GetSaveSubname(saveSelected));
	}
	else
	{
		nameTxt->SetText(CurrentBrowser()->GetItemDisplayName(saveSelected));
		
		if(CurrentBrowser()->IsDataBin(saveSelected))
		{
			databinInfos = WiiSaveCrypted::GetInfosAndBanner(CurrentBrowser()->GetCurrentSelectedFilepath(), dataBinBanner);
			subnameTxt->SetText(databinInfos->subname);
		}
		else
		{
			std::string savepath = CurrentBrowser()->GetCurrentPath();
			savepath += "/";
			savepath += CurrentBrowser()->GetItemName(saveSelected);
			subnameTxt->SetText(SaveTools::GetDeviceSubname(savepath));
		}
	}
	
	LoadBannerImage();
	SetBoxes();
	
	if(databinInfos)
	{
		delete databinInfos;
		databinInfos = NULL;
	}
}

void SaveInfos::LoadBannerImage()
{
	if(saveBanner)
	{
		Remove(saveBanner);
		delete saveBanner;
		saveBanner = NULL;
	}
	
	std::string savepath = "";
	ByteArray buffer;
	
	if(Settings.LoadedBrowser == SAVE_BROWSER)
	{
		m_tid = CurrentBrowser()->GetSaveTid(saveSelected);
		savepath = fmt("/title/%08x/%08x/data/banner.bin", TITLE_UPPER(m_tid), TITLE_LOWER(m_tid));
		Nand::LoadFile(savepath, buffer);
	}
	else
	{
		if (Settings.LoadedBrowser == SDUSB_BROWSER &&
			CurrentBrowser()->IsDataBin(saveSelected))
		{
			buffer = dataBinBanner;
		}
		else
		{
			if(Settings.LoadedBrowser == SDUSB_BROWSER)
			{
				savepath = CurrentBrowser()->GetCurrentPath();
				savepath += "/";
				savepath += CurrentBrowser()->GetItemName(saveSelected);
				savepath += "/banner.bin";
			}
			else
			{
				m_tid = CurrentBrowser()->GetSaveTid(saveSelected);
				savepath = CurrentBrowser()->GetCurrentPath();
				savepath += fmt("/title/%08x/%08x/data/banner.bin", TITLE_UPPER(m_tid), TITLE_LOWER(m_tid));
			}
			
			LoadFileToMem(savepath, buffer);
		}
	}
	
	saveBanner = new GuiSaveBanner(buffer);
	saveBanner->SetPosition(30,20);
	Append(saveBanner);
}

void SaveInfos::SetBoxes()
{
	if(idBox)
	{
		Remove(idBox);
		delete idBox;
		idBox = NULL;
	}
	if(blockBox)
	{
		Remove(blockBox);
		delete blockBox;
		blockBox = NULL;
	}
	
	char blockTxt[10];
	
	if(Settings.LoadedBrowser != SDUSB_BROWSER)
	{
		m_id = CurrentBrowser()->GetSaveID(saveSelected);
		
		snprintf(blockTxt, sizeof(blockTxt), "%0.f", ceil(CurrentBrowser()->GetSaveBlocks(saveSelected)));
	}
	else
	{
		float blocksize = 0;
		if(CurrentBrowser()->IsDataBin(saveSelected))
		{
			m_tid = databinInfos->tid;
			
			blocksize = ((float)((float)databinInfos->decryptedSize/(float)NAND_BLOCK_SIZE));
		}
		else
		{
			m_tid = SaveTools::StrToHex64(CurrentBrowser()->GetItemName(saveSelected));
			
			u64 foldersize = 0;
			u32 filenumber = 0;
			GetFolderSize(CurrentBrowser()->GetCurrentSelectedFilepath(), &foldersize, &filenumber);
			blocksize = ((float)((float)foldersize/(float)NAND_BLOCK_SIZE));
		}
		
		m_id = SaveTools::titleText(TITLE_UPPER(m_tid), TITLE_LOWER(m_tid));
		snprintf(blockTxt, sizeof(blockTxt), "%0.f", ceil(blocksize));
	}
	
	idBox = new GuiBlockBox(m_id);
	idBox->SetPosition(260, 27);
	Append(idBox);
	
	blockBox = new GuiBlockBox(blockTxt);
	blockBox->SetPosition(260+idBox->GetWidth()+20, 27);
	Append(blockBox);
}
